/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import dao.exceptions.NonexistentEntityException;
import entities.Testcase;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entities.Problem;

/**
 *
 * @author sbaldrich
 */
public class TestcaseJpaController {

    public TestcaseJpaController()
    {
        emf = Persistence.createEntityManagerFactory("CMalphaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager()
    {
        return emf.createEntityManager();
    }

    public void create(Testcase testcase)
    {
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            Problem problem = testcase.getProblem();
            if (problem != null)
            {
                problem = em.getReference(problem.getClass(), problem.getIdProblem());
                testcase.setProblem(problem);
            }
            em.persist(testcase);
            if (problem != null)
            {
                problem.getTestcaseList().add(testcase);
                problem = em.merge(problem);
            }
            em.getTransaction().commit();
        } finally
        {
            if (em != null)
            {
                em.close();
            }
        }
    }

    public void edit(Testcase testcase) throws NonexistentEntityException, Exception
    {
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            Testcase persistentTestcase = em.find(Testcase.class, testcase.getIdTestCase());
            Problem problemOld = persistentTestcase.getProblem();
            Problem problemNew = testcase.getProblem();
            if (problemNew != null)
            {
                problemNew = em.getReference(problemNew.getClass(), problemNew.getIdProblem());
                testcase.setProblem(problemNew);
            }
            testcase = em.merge(testcase);
            if (problemOld != null && !problemOld.equals(problemNew))
            {
                problemOld.getTestcaseList().remove(testcase);
                problemOld = em.merge(problemOld);
            }
            if (problemNew != null && !problemNew.equals(problemOld))
            {
                problemNew.getTestcaseList().add(testcase);
                problemNew = em.merge(problemNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex)
        {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0)
            {
                Integer id = testcase.getIdTestCase();
                if (findTestcase(id) == null)
                {
                    throw new NonexistentEntityException("The testcase with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally
        {
            if (em != null)
            {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException
    {
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            Testcase testcase;
            try
            {
                testcase = em.getReference(Testcase.class, id);
                testcase.getIdTestCase();
            } catch (EntityNotFoundException enfe)
            {
                throw new NonexistentEntityException("The testcase with id " + id + " no longer exists.", enfe);
            }
            Problem problem = testcase.getProblem();
            if (problem != null)
            {
                problem.getTestcaseList().remove(testcase);
                problem = em.merge(problem);
            }
            em.remove(testcase);
            em.getTransaction().commit();
        } finally
        {
            if (em != null)
            {
                em.close();
            }
        }
    }

    public List<Testcase> findTestcaseEntities()
    {
        return findTestcaseEntities(true, -1, -1);
    }

    public List<Testcase> findTestcaseEntities(int maxResults, int firstResult)
    {
        return findTestcaseEntities(false, maxResults, firstResult);
    }

    private List<Testcase> findTestcaseEntities(boolean all, int maxResults, int firstResult)
    {
        EntityManager em = getEntityManager();
        try
        {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Testcase.class));
            Query q = em.createQuery(cq);
            if (!all)
            {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally
        {
            em.close();
        }
    }

    public Testcase findTestcase(Integer id)
    {
        EntityManager em = getEntityManager();
        try
        {
            return em.find(Testcase.class, id);
        } finally
        {
            em.close();
        }
    }

    public int getTestcaseCount()
    {
        EntityManager em = getEntityManager();
        try
        {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Testcase> rt = cq.from(Testcase.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally
        {
            em.close();
        }
    }

}
